package com.platform.modules.plug.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.ObjectUtil;
import com.platform.common.exception.BaseException;
import com.platform.common.shiro.ShiroUtils;
import com.platform.common.version.ApiVersion;
import com.platform.common.version.VersionEnum;
import com.platform.common.web.domain.AjaxResult;
import com.platform.modules.chat.domain.ChatGroup;
import com.platform.modules.chat.domain.ChatGroupInfo;
import com.platform.modules.chat.domain.ChatUser;
import com.platform.modules.chat.service.ChatGroupInfoService;
import com.platform.modules.chat.service.ChatGroupService;
import com.platform.modules.chat.service.ChatUserService;
import com.platform.modules.chat.vo.MyVo09;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/open")
public class GroupOpenController {

    @Resource
    private ChatGroupService chatGroupService;
    @Resource
    private ChatGroupInfoService chatGroupInfoService;
    @Resource
    private ChatUserService chatUserService;

    /**
     * 获取群详情
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @GetMapping("/getInfo/{groupId}")
    public AjaxResult getInfo(@PathVariable Long groupId) {

        // 校验
        ChatGroup group = chatGroupService.getById(groupId);
        if (group == null) {
            throw new BaseException("当前群不存在");
        }

        if (!ObjectUtil.equal(group.getMaster(), ShiroUtils.getUserId())) {
            throw new BaseException("你不是群主，没有权限");
        }


        return AjaxResult.success(group);
    }

    /**
     * 获取自己创建的群列表
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @GetMapping("/getMyGroupList")
    public AjaxResult getMyGroupList() {

        List<ChatGroup> chatGroups = chatGroupService.getByMaster(ShiroUtils.getUserId());

        return AjaxResult.success(chatGroups);
    }

    /**
     * 获取自己的推荐人
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @GetMapping("/getInvitedMy")
    public AjaxResult getInvitedMy(@RequestParam("groupId") Long groupId,
                                   @RequestParam("userId") Long userId) {
        // 校验
        ChatGroup group = chatGroupService.getById(groupId);
        if (group == null) {
            throw new BaseException("当前群不存在");
        }
        if (!ObjectUtil.equal(group.getMaster(), ShiroUtils.getUserId())) {
            throw new BaseException("你不是群主，没有权限");
        }

        ChatGroupInfo groupInfo = chatGroupInfoService.getByUserId(groupId, userId);
        ChatUser chatUser = chatUserService.findById(groupInfo.getInvitedId());

        Dict dict = Dict.create()
                .set("nickName", chatUser.getNickName())
                .set("userId", chatUser.getUserId());

        return AjaxResult.success(dict);
    }


    /**
     * 获取我推荐的人
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @GetMapping("/getMyInvited")
    public AjaxResult getMyInvited(@RequestParam("groupId") Long groupId,
                                   @RequestParam("userId") Long userId) {

        ChatGroup group = chatGroupService.getById(groupId);
        if (group == null) {
            throw new BaseException("当前群不存在");
        }
        if (!ObjectUtil.equal(group.getMaster(), ShiroUtils.getUserId())) {
            throw new BaseException("你不是群主，没有权限");
        }

        List<ChatGroupInfo> byInvited = chatGroupInfoService.getByInvited(groupId, userId);
        List<Dict> list=new ArrayList<>();
        for (ChatGroupInfo groupInfo : byInvited) {

            ChatUser chatUser = chatUserService.findById(groupInfo.getUserId());
            Dict dict = Dict.create()
                    .set("nickName", chatUser.getNickName())
                    .set("userId", chatUser.getUserId());
            list.add(dict);
        }
        return AjaxResult.success(list);
    }
}
